<?php


class gdImage
{
    protected $type = "png";
    protected $im = null;
    protected $activeColor = null;
    protected $ttf = "arial.ttf";
    protected $jpeg_quality = '95';
    
    public static function getMimeFromExtension( $filename )
    {
        $res = preg_match( "/\.([\w]{3,4})/", $filename, &$results );
        if ( $res )
            return $results[1];
        else
            return -1;
    }
    
    public static function mergeImages( $filenames )
    {
        $retVal = null;
        
        foreach( $filenames as $mixed )
        {
            if ( is_array( $mixed ) )
            {
                
            }
            else
            {
                if ( $retVal == null )
                    $retVal = new gdImage( $mixed );
                else
                    $retVal->merge( new gdImage( $mixed ) );
            }
        }
        return $retVal;
    }
    
    public static function convertFileName( $string, $encoding = "UTF-8" )
    {
        mb_regex_encoding( $encoding );
        $string = trim( $string );
        $string = mb_ereg_replace( "á", "a", $string );
        $string = mb_ereg_replace( "Á", "A", $string );
        $string = mb_ereg_replace( "é", "e", $string );
        $string = mb_ereg_replace( "É", "E", $string );
        $string = mb_ereg_replace( "ö|ő|ó", "o", $string );
        $string = mb_ereg_replace( "Ö|Ő|Ó", "O", $string );
        $string = mb_ereg_replace( "ü|ű|ú", "u", $string );
        $string = mb_ereg_replace( "Ü|Ű|Ú", "U", $string );
        $string = mb_ereg_replace( "í", "i", $string );
        $string = mb_ereg_replace( "Í", "I", $string );
        $string = mb_ereg_replace( " ", "_", $string );
        $string = mb_ereg_replace( ",", "", $string );
        $string = mb_ereg_replace( ":", "", $string );
        $string = mb_ereg_replace( "\(", "", $string );
        $string = mb_ereg_replace( "\)", "", $string );
        $string = mb_strtolower( $string, $encoding );
        return $string;
        
    }
    
    
    public function __construct( $filename )
    {
        $this->type = self::getMimeFromExtension( $filename );
        if ( $this->type == "png" )
        {
            $this->im = imagecreatefrompng( $filename );
            imagesavealpha( $this->im, true );
        }
        else if ( $this->type == "jpg" || $this->type == "jpeg" )
        {
            $this->im = imagecreatefromjpeg( $filename );
        }
        else if ( $this->type == "gif" )
        {
            $this->im = imagecreatefromgif( $filename );
            imagesavealpha( $this->im, true );
        }
    }
    
    public function __destruct()
    {
        imagedestroy( $this->im );
    }
    
    public function getIm()
    {
        return $this->im;
    }
    
    public function merge( gdImage $newLayer )
    {
        imagecopy( $this->im, $newLayer->getIm(), 0, 0, 0, 0, imagesx($newLayer->getIm()), imagesx($newLayer->getIm()) );
        return $this;
    }
    
    public function setColor( $hex )
    {
        if ( !isset( $this->colors[$hex] ) )
        {
            $base = 0x0000000FF;
            
            $hexnum = hexdec( trim( $hex, "#" ) );
            
            $alpha = 0;
            $j = 0;
            
            if ( strlen( trim( $hex, "#" ) ) == 8 )
            {
                $alpha = (int)$hexnum & $base;
                $j = 1;
                $base = $base << 8;                
            }
                

            for ( $i = 0; $i < 3; $i++ )
            {   
                
                switch( $i )
                {
                    case 0:
                        $blue = ((int)$hexnum & $base ) >> ( 8 * ( $i + $j ) );
                    break;
                
                    case 1:
                        $green = ((int)$hexnum & $base ) >> ( 8 * ( $i + $j ) );
                    break;
                
                    case 2:
                        $red = ((int)$hexnum & $base ) >> (  8 * ( $i + $j ) );
                    break;
                }
                $base = $base << 8;                
            }
            
            $this->colors[$hex] = imagecolorallocatealpha( $this->im, $red, $green, $blue, $alpha );
        }
        $this->activeColor = $this->colors[$hex];        
        return $this;
    }
    
    public function writeTTFText( $text, $size = 12, $angle = 0, $align = 'left', $valign = 'top' )
    {
        $rect = imagettfbbox( $size, 0, $this->ttf, $text );
        
        $a = deg2rad($angle);
        // compute some usefull values
        $ca = cos($a);
        $sa = sin($a);
        // perform transformations
        
        $temp = array();
        for($i = 0; $i < 7; $i += 2){
            $temp[$i] = round($rect[$i] * $ca + $rect[$i+1] * $sa);
            $temp[$i+1] = round($rect[$i+1] * $ca - $rect[$i] * $sa);
        }


        $rect = $temp;
                        
        $boundRect = array();
        
        for( $i = 0; $i < 8; $i++ )
        {
            if ( $i % 2 == 0 )
            {
                if ( !isset( $boundRect['l'] ) || $boundRect['l'] > $rect[$i] )
                    $boundRect['l'] = $rect[$i];
                if ( !isset( $boundRect['r'] ) || $boundRect['r'] < $rect[$i] )
                    $boundRect['r'] = $rect[$i];                    
            }
            else
            {
                if ( !isset( $boundRect['t'] ) || $boundRect['t'] > $rect[$i] )
                    $boundRect['t'] = $rect[$i];
                if ( !isset( $boundRect['b'] ) || $boundRect['b'] < $rect[$i] )
                    $boundRect['b'] = $rect[$i];                                    
            }
        }
        
        $boundRect['width'] = $boundRect['r'] - $boundRect['l'];
        $boundRect['height'] = $boundRect['b'] - $boundRect['t'];
        
        if ( $align == 'left' )
        {
            $x = -$boundRect['l'];
        }
        else if ( $align == 'right' )
        {
            $x = imagesx( $this->im ) - $boundRect['r']-2;
        }
        else if( $align == 'center' )
        {
            $x = ( imagesx( $this->im ) - $boundRect['width'] ) / 2;
            $x = $x - $boundRect['l'];
        }

        if ( $valign == 'top' )
        {
            $y = -$boundRect['t'];
        }
        else if ( $valign == 'bottom' )
        {
            $y = imagesy( $this->im ) - $boundRect['b'];
        }
        else if( $valign == 'middle')
        {
            $y = ( imagesy( $this->im ) - $boundRect['height'] ) / 2 - $boundRect['t'];
        }

        
        foreach( $rect as $key => $value )
        {
            if ( $key % 2 == 0 )
                $rect[$key] += $x;
            else
                $rect[$key] += $y;
        }
    
        //imagepolygon( $this->im, $rect, 4, $this->activeColor );
        //imagerectangle( $this->im, $x + $boundRect['l'], $y + $boundRect['t'],  $x + $boundRect['r'], $y + $boundRect['b'], $this->activeColor );
        imagettftext( $this->im, $size, $angle, $x, $y, $this->activeColor, $this->ttf, $text );
        return $this;
    }
    
    public function show()
    {
        if ( $this->type == "png" )
            imagepng( $this->im );
        else if( $this->type == "jpg" || $this->type == "jpeg" )
            imagejpeg( $this->im, null, $this->jpeg_quality );
        else if ( $this->type == "gif" )
            imagegif( $this->im );
    }
    
    
}

?>